<?php
namespace jy_farmer;
defined('BY_JYA') or exit('error');
/*
	优惠券
*/
class coupon extends \plugin{
	public function getmycoupon_ing($coupontype=1,$goodsprice=0,$level_de_price='',$orders='',$enough_de_price=0,$page=0,$mid=0){
		//整理优惠券数据，先获取用户所有待使用优惠券，再从中筛选出未过期的优惠券且本次下单可用的优惠券！
		$coupons=model('jy_weishop/coupon')->getmycoupon('','','',$page,$mid);
		$couponset=$this->plugin_setting('coupon');
		if($coupons){
			$newcoupons=array();
			foreach($coupons as $key=>$coupon){
				$coupon_detail=pdo_get('jy_weishop_coupon',array('id'=>$coupon['couponid'],'coupontype'=>$coupontype,'uniacid'=>$_SESSION['uniacid']));
				if($coupon_detail['backtype']==2){
					$coupon_detail['deduct']=0;
				}
				if(!$coupon_detail){
					unset($coupons[$key]);
					continue;
				}
				if(!$coupon_detail['status']){
					//优惠券状态为不可用
					//unset($coupons[$key]);
					//continue;
				}
				//判断使用限制
				if($coupon_detail['limitusetype']){
					switch($coupon_detail['limitusetype']){
						case 1://不可与促销优惠同时使用
							if($enough_de_price){
								unset($coupons[$key]);
								continue;
							}
						case 2://不可与会员折扣同时使用
							if($level_de_price){
								unset($coupons[$key]);
								continue;
							}
						case 3://不可与促销、折扣同时使用
							if($level_de_price){
								unset($coupons[$key]);
								continue;
							}
							if($enough_de_price){
								unset($coupons[$key]);
								continue;
							}
					}
				}
				//判断有效期
				if($coupon_detail['timelimittype']==1){
					//有效期类型为领取后若干天内使用
					$days=(TIMESTAMP-$coupon['createtime'])/86400;
					if($coupon_detail['timedays']<$days){
						//已过期
						unset($coupons[$key]);
						continue;
					}
				}elseif($coupon_detail['timelimittype']==2){
					$coupon_detail['limittimes']=json_decode($coupon_detail['limittimes'],true);
					if(TIMESTAMP<$coupon_detail['limittimes']['tstart'] || TIMESTAMP>$coupon_detail['limittimes']['tend']){
						//不在有效期内
						unset($coupons[$key]);
						continue;
					}
				}
				if($coupontype==1){
					$neworders=$orders;
					if($coupon_detail['limitgoodtype']){
						unset($coupons[$key]);
						continue;	
					}
					if($coupon_detail['limitgoodcatetype']){
						unset($coupons[$key]);
						continue;
					}
					//多商户
					if($coupon_detail['ismerch']==1 && $coupon_detail['merchtype']){
						unset($coupons[$key]);
						continue;
					}
					if($goodsprice && $coupon_detail['enough'] && $goodsprice<$coupon_detail['enough']){
						//不可用
						unset($coupons[$key]);
						continue;
					}
				}
				switch($coupon_detail['backtype']){
					case 0://立减
						$coupon['de_price']=$coupon_detail['deduct'];
						$coupon['content']="满{$coupon_detail['enough']}减{$coupon_detail['deduct']}";
						break;
					case 1://折扣
						$disrate=100-$coupon_detail['discount']*10;
						$coupon['de_price']=intval($disrate*$goodsprice)/100;
						$coupon['content']="满{$coupon_detail['enough']}打{$coupon_detail['discount']}折";
						break;
					case 2://返利
						$coupon['de_price']=0;
						$coupon['content']="满{$coupon_detail['enough']}返{$coupon_detail['backmoney']}余额，{$coupon_detail['backcredit']}积分，{$coupon_detail['backredpack']}现金";
						break;
					default:
						$coupon['de_price']=0;
						$coupon['content']='';
				}
				$coupon=array_merge($coupon_detail,$coupon);
				if($coupons[$key]){
					$newcoupons[]=$coupon;
				}
			}
		}
		if(!empty($newcoupons)){
			//存在可用优惠券，选择一个抵扣金额最大的作为默认选中的优惠券
			paixu($newcoupons,'de_price','desc');
		}
		return $newcoupons;
	}
}